/**************************************************************************//**
  \file blink.c

  \brief Blink application.

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2012, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
******************************************************************************/

#include <stdio.h>
#include <appTimer.h>
#include <zdo.h>
#include <blink.h>
#include <taskManager.h>
#include <debug.h>

static HAL_AppTimer_t blinkTimer;                           // Blink timer.

//static void buttonsReleased(uint8_t buttonNumber);          // Button release event handler.
static void blinkTimerFired(void);                          // blinkTimer handler.

static char *msgError = "Error";
static char msgTemp[150];

static void printTemp2USART(bool result, int16_t data);

/*******************************************************************************
  Description: application task handler.

  Parameters: none.

  Returns: nothing.
*******************************************************************************/
void APL_TaskHandler(void)
{
  DBG_USART_Open();
  BSP_OpenLeds(); // Enable LEDs
  BSP_OpenTemperatureSensor(); // Enable Temperature Sensor

  BSP_OnLed(LED_RED);
  BSP_OnLed(LED_YELLOW);
  BSP_OnLed(LED_GREEN);

  //BSP_OpenButtons(NULL, buttonsReleased);         // Register button event handlers
  // Configure blink timer
  //changeBlinkTimer.interval = 10000;              // Timer interval
  //changeBlinkTimer.mode     = TIMER_REPEAT_MODE;  // Repeating mode (TIMER_REPEAT_MODE or TIMER_ONE_SHOT_MODE)
  //changeBlinkTimer.callback = changeTimerFired;   // Callback function for timer fire event
  //HAL_StartAppTimer(&changeBlinkTimer);           // Start blink timer      */
  // Configure blink timer
  blinkTimer.interval = APP_BLINK_INTERVAL;       // Timer interval
  blinkTimer.mode     = TIMER_REPEAT_MODE;        // Repeating mode (TIMER_REPEAT_MODE or TIMER_ONE_SHOT_MODE)
  blinkTimer.callback = blinkTimerFired;          // Callback function for timer fire event
  HAL_StartAppTimer(&blinkTimer);                 // Start blink timer
}

/*******************************************************************************
  Description: blinking timer fire event handler.

  Parameters: none.

  Returns: nothing.
*******************************************************************************/
static void blinkTimerFired()
{
	BSP_ToggleLed(LED_RED);
	BSP_ToggleLed(LED_YELLOW);
	BSP_ToggleLed(LED_GREEN);
	BSP_ReadTemperatureData(printTemp2USART);
}


static void printTemp2USART(bool result, int16_t data)
{
	int n;
	if (!result)
	{
		DBG_USART_Write((uint8_t*)msgError, sizeof(msgError));
	}
	else
	{
		n = sprintf(msgTemp,"Temperatura = %3.2f\n",data*128.0/255.0);
		DBG_USART_Write((uint8_t*)msgTemp, sizeof(msgTemp));
	}
}


/*******************************************************************************
  Description: just a stub.

  Parameters: are not used.

  Returns: nothing.
*******************************************************************************/
void ZDO_MgmtNwkUpdateNotf(ZDO_MgmtNwkUpdateNotf_t *nwkParams)
{
  nwkParams = nwkParams;  // Unused parameter warning prevention
}

/*******************************************************************************
  Description: just a stub.

  Parameters: none.

  Returns: nothing.
*******************************************************************************/
void ZDO_WakeUpInd(void)
{
}

#ifdef _BINDING_
/***********************************************************************************
  Stub for ZDO Binding Indication

  Parameters:
    bindInd - indication

  Return:
    none

 ***********************************************************************************/
void ZDO_BindIndication(ZDO_BindInd_t *bindInd)
{
  (void)bindInd;
}

/***********************************************************************************
  Stub for ZDO Unbinding Indication

  Parameters:
    unbindInd - indication

  Return:
    none

 ***********************************************************************************/
void ZDO_UnbindIndication(ZDO_UnbindInd_t *unbindInd)
{
  (void)unbindInd;
}
#endif //_BINDING_

/**********************************************************************//**
  \brief Main - C program main start function

  \param none
  \return none
**************************************************************************/
int main(void)
{
  SYS_SysInit();

  for(;;)
  {
    SYS_RunTask();
  }
}

//eof blink.c
